package cn.yunhe.service.impl;

import cn.yunhe.dao.NewsDao;
import cn.yunhe.entity.News;
import cn.yunhe.service.NewsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("newsService")
public class NewsServiceImpl implements NewsService {
    @Resource
    private NewsDao newsDao;


    @Override
    public Page getNewsListByPage(Map<String, Object> map) {
        Page page = new Page();

        //获取总条数
        page.setTotal(newsDao.getNewsListCountByPage(map));
        // System.out.println(knowledgePointDao.getKnowledgePointListCountByPage(map));
        // 获取每页大小
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        page.setPageSize(pageSize);

        // 获取当前页码
        int curPageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
//		page.setPageNum(curPageNum);
        // 计算出总页数
        page.setPageNum((int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1)));
        // 启用分页
        page = PageHelper.startPage(curPageNum, pageSize);
        // 查询列表
        List list = newsDao.getNewsListByPage(map);
        return page;
    }



    @Override
    public int addNews(News news) {
        news.setCreateTime(new Date());
        if (news.getTitle() == null && news.getContent() == null) {
            return 0;
        } else {
          //  System.out.println("news     " + news.getTitle());
            return newsDao.addNews(news);
        }
    }

    @Override
    public int delNews(News news) {
      //  System.out.println("id  "+news.getNewsId());
        return newsDao.delNews(news);
    }

    @Override
    public int updNews(News news) {
        if (news.getTitle() == null&&news.getContent()==null) {
            return 0;
        } else {
          //  System.out.println("news     " + news.getTitle());
            return newsDao.updNews(news);
        }
    }

    @Override
    public News getNewsById(News news) {
        return newsDao.getNewsById(news);
    }


    public NewsDao getNewsDao() {
        return newsDao;
    }

    public void setNewsDao(NewsDao newsDao) {
        this.newsDao = newsDao;
    }
}
